library(dplyr)
library(ggthemes)
library(ggplot2)
library(readxl)
library(cowplot)
library(lubridate)

## DAFTAR 

data <- read_excel("data/raw/sectarian_DADRI/sectarian_Violevnce_observatory_2008-2020_academic_version.xlsx", 
                   skip = 1)

data$date <- as.Date(as.integer(data$`تاريخ الواقعة`), origin = "1899-12-30")

copt_vio <- data %>%
  filter(`ديانة المرتكب ضده الفعل`== "قبطي")

copt_vio <- copt_vio %>%
  filter(!is.na(date))

copt_viof <- copt_vio %>%
  mutate(event = 1) %>%
  select(event, date) %>%
  tidyr::complete(date = seq.Date(min(date), max(date), by="day")) %>%
  mutate(event = case_when(is.na(event) ~ 0,
                             TRUE ~ event))

copt_viof$yearmon <- tsibble::yearmonth(copt_viof$date)
copt_viof$yearmond <- as.Date(copt_viof$yearmon)
copt_viof$year <- year(copt_viof$date)

g1 <- copt_viof %>%
  group_by(year) %>%
  summarise(sum_vio = sum(event)) %>%
  ggplot() +
  geom_bar(aes(year, sum_vio), stat = "identity") +
  theme_tufte(base_family = "Helvetica") +
  labs(x = "Year", y = "# events", title = "DADRI")

copt_viofstata <- copt_viof %>%
  group_by(date) %>%
  summarise(sum_vio = sum(event))
  
# haven::write_dta(copt_viofstata, "/Users/cbarrie6/Dropbox/pd_projects/sectarian_paper/analysis/DADRI_copt_vio.dta")


## SCAD 

data <- read.csv("data/raw/sectarian_SCAD/SCAD2018Africa_Final.csv")

data$date <- as.Date(data$startdate, format = "%d-%b-%y")

eth_viof <- data %>%
  mutate(event = 1) %>%
  filter(countryname == "Egypt" & 
           issue1==5|issue2==5|issue3==5|
           issue1==6|issue2==6|issue3==6)
  

eth_viof$target <- tolower(eth_viof$target1)

eth_viof <- eth_viof %>%
  filter(grepl("copt", target))


eth_viof <- eth_viof %>%
  tidyr::complete(date = seq.Date(min(date), max(date), by="day")) %>%
  mutate(event = case_when(is.na(event) ~ 0,
                           TRUE ~ event))

eth_viof$yearmon <- tsibble::yearmonth(eth_viof$date)
eth_viof$yearmond <- as.Date(eth_viof$yearmon)
eth_viof$year <- lubridate::year(eth_viof$yearmon)

g2 <- eth_viof %>%
  group_by(year) %>%
  summarise(sum_vio = sum(event)) %>%
  mutate(sum_vio = case_when(is.na(sum_vio) ~ 0,
                             TRUE ~ sum_vio)) %>%
  ggplot() +
  geom_bar(aes(year, sum_vio), stat = "identity") +
  theme_tufte(base_family = "Helvetica") +
  scale_y_continuous(breaks = c(0,5,10)) +
  labs(x = "Year", y = "# events", title = "SCAD")

## GTD

# ***data only includes organized terror attacks***
data <- read_excel("data/raw/sectarian_GTD/globalterrorismdb_0221dist.xlsx")

eth_viof <- data %>%
  mutate(event = 1) %>%
  filter(country_txt == "Egypt" & 
           targtype1 == 15|
           country_txt == "Egypt" & 
           targtype1 == 14)

writexl::write_xlsx(eth_viof, "data/raw/sectarian_GTD/GTD_egypt.xlsx")
#hand code anti-Christian events as copt column
eth_viof <- read_excel("data/raw/sectarian_GTD/GTD_egypt_coded.xlsx")

eth_viof <- eth_viof %>%
  filter(copt==1)

eth_viof$date <- as.Date(paste0(eth_viof$iyear,"-", eth_viof$imonth, "-", eth_viof$iday))

eth_viof <- eth_viof %>%
  tidyr::complete(date = seq.Date(min(date), max(date), by="day")) %>%
  mutate(event = case_when(is.na(event) ~ 0,
                           TRUE ~ event))

eth_viof$yearmon <- tsibble::yearmonth(eth_viof$date)
eth_viof$yearmond <- as.Date(eth_viof$yearmon)
eth_viof$year <- lubridate::year(eth_viof$yearmon)

g3 <- eth_viof %>%
  group_by(year) %>%
  summarise(sum_vio = sum(event)) %>%
  mutate(sum_vio = case_when(is.na(sum_vio) ~ 0,
                             TRUE ~ sum_vio)) %>%
  ggplot() +
  geom_bar(aes(year, sum_vio), stat = "identity") +
  theme_tufte(base_family = "Helvetica") +
  labs(x = "Year", y = "# events", title = "GTD")


pdf("plots/Figure_C1_pretrends.pdf")
plot_grid(g1, g2,g3, 
          labels = c('A', 'B', 'C'), 
          label_size = 12, ncol = 1)
dev.off()